package web.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**日志工具类
 * @author 樂淵
 * @version 1.0
 * */
public class Log {
	
	/**设置日志信息，存到项目根目录的log目录下
	 * @param msg 日志信息
	 * */
	public void setLog(String msg){
		msg = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss# ").format(new Date())+msg;
		System.out.println(msg);
		
		String path = getClass().getClassLoader().getResource("").getPath();
		path=path.substring(0,path.lastIndexOf("/"));
		path=path.substring(0,path.lastIndexOf("/"));
		path=path.substring(0,path.lastIndexOf("/"));
		String folderPath = path+"/log";
		path+="/log/log"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".txt";
		
		File folder = new File(folderPath);
		if(!folder.exists()){
			folder.mkdirs();
		}
		
		try {
			File file = new File(path);
			FileWriter fw = new FileWriter(file, true);
			
			if(file.exists()){
				fw.append(msg+"\n");
				fw.close();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
